package com.neuqsoft.ymtdemo.utils;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.neuqsoft.ymtdemo.common.ApiNeuqsoft;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import essclib.pingan.ai.request.biap.Biap;
import essclib.pingan.ai.request.biap.net.OkHttpUtils;
import com.neuqsoft.ymtdemo.App;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * <pre>
 *     author : wulh
 *     e-mail : wulh@neuqsoft.com
 *     time   : 2020/03/23
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class RequestUtil {
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static void getCardRecord(String idcard, String name, String aab301, final CallBackNet callBackNet) {
        final Request request;
        request = new Request.Builder()
                .url(ApiNeuqsoft.URL_TEST + ApiNeuqsoft.CARD_RECORD + "?channelNo=" + App.channelNo + "&idcard=" + idcard + "&name=" + name + "&aab301=" + aab301)
                .get()
                .build();
        OkHttpUtils.getInstance().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBackNet.onFail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (callBackNet != null) {
                        callBackNet.onSuccess(response.body().string());
                    }
                }
            }
        });
    }

    //不带用户信息只传姓名，社保卡号，ak
    public static void sign(String aac002, String aac003,String aac067, final CallBackNet callBackNet) {
        sign(aac002, aac003, null, null, aac067, null, null, null, null, null, callBackNet);
    }

    //带用户信息，多传：发卡地，签发号，手机号aac067 历史数据标志historyFlag 支付必传openID
    public static void sign(String aac002, String aac003, String aab301, String signNo,
                            String aac067, String account_id_hash, String openID, String isIndep, String qrCode, String appId, final CallBackNet callBackNet) {

        Map data = new HashMap();
        data.put("accessKey", App.accessKey);
        data.put("channelNo", App.channelNo);
        if (!TextUtils.isEmpty(aab301)) {
            data.put("aab301", aab301);
        }
        data.put("aac002", aac002);
        data.put("aac003", aac003);
        data.put("signNo", signNo);
        data.put("aac067", aac067);
        // 历史数据标志historyFlag:传1为历史数据
        data.put("account_id_hash", account_id_hash);
        if (!TextUtils.isEmpty(openID)) {
            data.put("openId", openID);
        }
        //是否为独立服务，传1为是，不传为不是
        data.put("isIndep", isIndep);
        //是否有资格做待遇资格认证，若1年内做过，则不重复测
        data.put("qrCode", qrCode);

        data.put("appId", appId);

        if (!TextUtils.isEmpty(App.isAuthFace)) {
            data.put("isAuthFace", App.isAuthFace);
        }
        data.put("return_url", "");
        String sign = Biap.getInstance().getSign();
        RequestBody body = RequestBody.create(JSON, JSONObject.toJSONString(data));
        Request request = new Request.Builder()
                .url(sign)
                .post(body)
                .build();
        OkHttpUtils.getInstance().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (callBackNet != null) {
                    callBackNet.onFail(e.getMessage());
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (callBackNet != null) {
                        Map<String, String> result = JSONObject.parseObject(response.body().string(), Map.class);
                        callBackNet.onSuccess(result.get("result"));
                    }
                }
            }
        });
    }

    public static void singUp(String idcard, String name, final CallBackNet callBackNet) {
        final Request request;
        Map data = new HashMap();
        data.put("signNo", App.signNO);
        data.put("idcard", idcard);
        data.put("name", name);
        request = new Request.Builder()
                .url(ApiNeuqsoft.URL_TEST + ApiNeuqsoft.SIGN_UP)
                .addHeader("X-Client-Name", App.x_client_name)
                .post(RequestBody.create(JSON, JSONObject.toJSONString(data)))
                .build();
        OkHttpUtils.getInstance().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBackNet.onFail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (callBackNet != null) {
                        Map<String, String> result = JSONObject.parseObject(response.body().string(), Map.class);
                        callBackNet.onSuccess(result.get("code"));
                    }
                }
            }
        });
    }

    public static void getApply(String idcard, final CallBackNet callBackNet) {
        final Request request;
        request = new Request.Builder()
                .url(ApiNeuqsoft.URL_TEST + ApiNeuqsoft.GET_APPLY + "?idcard=" + idcard + "&channelNo=" + App.channelNo)
                .get()
                .build();
        OkHttpUtils.getInstance().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBackNet.onFail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (callBackNet != null) {
                        callBackNet.onSuccess(response.body().string());
                    }
                } else {
                    Map<String, String> result = JSONObject.parseObject(response.body().string(), Map.class);
                    callBackNet.onFail(result.get("message"));
                }
            }
        });
    }
    public static void potApply(String data, final CallBackNet callBackNet) {
        final Request request;
        request = new Request.Builder()
                .url(ApiNeuqsoft.URL_TEST + ApiNeuqsoft.POST_APPLY )
                .post(RequestBody.create(JSON, data))
                .build();
        OkHttpUtils.getInstance().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBackNet.onFail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (callBackNet != null) {
                        Map<String, String> result = JSONObject.parseObject(response.body().string(), Map.class);
                        callBackNet.onFail(result.get("message"));
                    }
                } else {
                    Map<String, String> result = JSONObject.parseObject(response.body().string(), Map.class);
                    callBackNet.onFail(result.get("message"));
                }
            }
        });
    }
    public static void login(String busiSeq, String signNo, final CallBackNet callBackNet) {
        final Request request;
        Map data = new HashMap();
        data.put("busiSeq", busiSeq);
        data.put("channelNo", App.channelNo);
        data.put("signNo", signNo);
        request = new Request.Builder()
                .url(ApiNeuqsoft.URL_TEST + ApiNeuqsoft.LOGIN)
                .post(RequestBody.create(JSON, JSONArray.toJSONString(data)))
                .build();
        OkHttpUtils.getInstance().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBackNet.onFail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (callBackNet != null) {
                        //登录成功返回信息
                        Map<String, String> result = JSONObject.parseObject(response.body().string(), Map.class);
                        callBackNet.onSuccess(result.get("message"));
                    }
                }
            }
        });
    }

    public interface CallBackNet {

        void onSuccess(String data);

        void onFail(String data);
    }

}
